package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.GeneratedEntry;
import hu.rsc.feeds.client.data.GeneratedFeed;
import hu.rsc.feeds.client.gui.FeedPanel;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;

public class GeneratedPanel extends DialogBox implements FeedPanel {
    private GeneratedTable table;
    private GeneratedForm form;

    private GeneratedEntryTable entrytable;
    private GeneratedEntryForm entryform;

    public GeneratedPanel() {
	super(true);
	setText(Dictionary.getEntry(Dictionary.TITLE_OWN_FEEDS, this));
	addStyleName("body");
	setAnimationEnabled(true);
	FlexTable main = new FlexTable();
	this.table = new GeneratedTable(this);
	main.setWidget(0, 0, this.form = new GeneratedForm(this));
	main.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
	main.setWidget(0, 1, this.table);

	main.setWidget(1, 0, this.entryform = new GeneratedEntryForm(this));
	main.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
	main.setWidget(1, 1, this.entrytable = new GeneratedEntryTable(this));
	setWidget(main);
    }

    public GeneratedFeed getFeed() {
	return this.form.getFeed();
    }

    @Override
    public Long getFeedId() {
	return getFeed().getId();
    }

    @Override
    public void refreshEntityTable() {
	GeneratedPanel.this.entrytable.refreshTable();
    }

    @Override
    public void refreshEntryForm(GeneratedEntry entry) {
	this.entryform.refresh(entry);
    }

    @Override
    public void refreshFeedsTable() {
	this.table.refreshTable();
    }

    public void refreshForm(GeneratedFeed feed) {
	this.form.refresh(feed);
	this.entrytable.refreshTable();
	this.entryform.refresh(new GeneratedEntry());
    }

    public void save(GeneratedFeed feed) {
	Feeds.getService().saveGeneratedFeed(feed, new AsyncCallback<GeneratedFeed>() {

	    @Override
	    public void onFailure(Throwable caught) {
		Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_COMM_ERROR, null));
	    }

	    @Override
	    public void onSuccess(GeneratedFeed result) {
		GeneratedPanel.this.table.refreshTable();
		GeneratedPanel.this.form.refresh(result);
	    }
	});
    }

    @Override
    public void saveEntry(GeneratedEntry entry) {
	Feeds.getService().saveGeneratedEntry(entry, new AsyncCallback<GeneratedEntry>() {

	    @Override
	    public void onFailure(Throwable caught) {
		Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_COMM_ERROR, null));
	    }

	    @Override
	    public void onSuccess(GeneratedEntry result) {
		GeneratedPanel.this.entrytable.refreshTable();
		GeneratedPanel.this.entryform.refresh(result);
	    }
	});
    }
}
